/* MOS                                                        */
/* Giordano Mion, Luca David Opromolla and Alessandro Sforza  */
/* University of Sussex, Banco de Portugal, and LSE           */


program drop _all
clear all
capture log close
set more off
local path = "/Users/lucadavidopromolla/Projects/MOS_rev"
cd "`path'/computation"
log using "`path'/logs/MOS_ac_Apr_11_2022_rev.log", replace

set mem 10g


// Prepare data on armed conflict
use "`path'/data/ac/ucdp-prio-acd-172.dta",clear
drop if location =="South Sudan" | location =="Serbia (Yugoslavia)"
drop if location=="Angola"
gen pais = 660 if location=="Afghanistan, United Kingdom, United States of America"
replace pais = 660 if location=="Afghanistan"
replace pais = 208 if location=="Algeria"
replace pais = 330 if location=="Angola"
replace pais = 612 if location =="Australia, Iraq, United Kingdom, United States of America"
replace pais = 10004 if location =="Azerbaijan"
replace pais = 666 if location =="Bangladesh"
replace pais = 93 if location =="Bosnia-Herzegovina"
replace pais = 328 if location =="Burundi"
replace pais = 696 if location =="Cambodia (Kampuchea)"
replace pais = 302 if location =="Cameroon"
replace pais = 306 if location =="Central African Republic"
replace pais = 244 if location =="Chad"
replace pais = 480 if location =="Colombia"
replace pais = 375 if location =="Comoros"
replace pais = 318 if location =="Congo"
replace pais = 322 if location =="DR Congo (Zaire)"
replace pais = 338 if location =="Djibouti"
replace pais = 220 if location =="Egypt"
replace pais = 336 if location =="Eritrea"
replace pais = 334 if location =="Ethiopia"
replace pais = 76 if location =="Georgia"
replace pais = 416  if location =="Guatemala"
replace pais = 260 if location =="Guinea"
replace pais = 257  if location =="Guinea-Bissau"
replace pais = 452 if location =="Haiti"
replace pais = 616 if location =="Iran"
replace pais = 700  if location =="Indonesia"
replace pais = 612  if location =="Iraq"
replace pais = 272 if location =="Ivory Coast"
replace pais = 628 if location =="Jordan"
replace pais = 346 if location =="Kenya"
replace pais = 604  if location =="Lebanon"
replace pais = 395 if location =="Lesotho"
replace pais = 268  if location =="Liberia"
replace pais = 216 if location =="Libya"
replace pais = 96 if location =="Macedonia, FYR"
replace pais = 701 if location =="Malaysia"
replace pais = 232 if location =="Mali"
replace pais = 228  if location =="Mauritania"
replace pais = 366  if location =="Mozambique"
replace pais = 676 if location =="Myanmar (Burma)"
replace pais = 240 if location =="Niger"
replace pais = 288 if location =="Nigeria"
replace pais = 672 if location =="Nepal"
replace pais = 662  if location =="Pakistan"
replace pais = 801  if location =="Papua New Guinea"
replace pais = 324 if location =="Rwanda"
replace pais = 248 if location =="Senegal"
replace pais = 608 if location =="Syria"
replace pais = 264 if location =="Sierra Leone"
replace pais = 342  if location =="Somalia"
replace pais = 10004 if location =="Uzbekistan"
replace pais = 224 if location =="Sudan"
replace pais = 72 if location =="Ukraine"
replace pais = 653 if location =="Yemen (North Yemen)"
replace pais = 669 if location =="Sri Lanka"
replace pais = 504  if location =="Peru"
replace pais = 10004  if location =="Tajikistan"
replace pais = 720  if location =="China"
replace pais = 92  if location =="Croatia"
replace pais = 664  if location =="India"
replace pais = 21  if location =="Israel"
replace pais = 412  if location =="Mexico"
replace pais = 708  if location =="Philippines"
replace pais = 10004 if location =="Russia (Soviet Union)"
replace pais = 680 if location =="Thailand"
replace pais = 52 if location =="Turkey"
replace pais = 350 if location =="Uganda"
replace pais = 6 if location =="United Kingdom"
replace pais = 400  if location =="United States of America"
replace pais = 224  if location =="South Sudan, Sudan"
expand = 2 if location =="Cambodia (Kampuchea), Thailand", gen(newvar)
replace pais = 696  if location =="Cambodia (Kampuchea), Thailand" & newvar==1
replace pais = 680  if location =="Cambodia (Kampuchea), Thailand" & newvar==0
drop newvar
expand = 2 if location =="Cameroon, Nigeria", gen(newvar)
replace pais = 302  if location =="Cameroon, Nigeria" & newvar==1
replace pais = 288  if location =="Cameroon, Nigeria" & newvar==0
drop newvar
expand = 2 if location =="Djibouti, Eritrea", gen(newvar)
replace pais = 338  if location =="Djibouti, Eritrea" & newvar==1
replace pais = 336  if location =="Djibouti, Eritrea" & newvar==0
drop newvar
expand = 2 if location =="Ecuador, Peru", gen(newvar)
replace pais = 500  if location =="Ecuador, Peru" & newvar==1
replace pais = 504  if location =="Ecuador, Peru" & newvar==0
drop newvar
expand = 2 if location =="Eritrea, Ethiopia", gen(newvar)
replace pais = 336  if location =="Eritrea, Ethiopia" & newvar==1
replace pais = 334  if location =="Eritrea, Ethiopia" & newvar==0
drop newvar
expand = 2 if location =="India, Pakistan", gen(newvar)
replace pais = 664  if location =="India, Pakistan" & newvar==1
replace pais = 662  if location =="India, Pakistan" & newvar==0
drop newvar
keep if year>=1991 & year<=2006
keep conflict_id location year pais region intensity_level cumulative_intensity
replace region = "2" if region=="1, 2, 3, 5"
replace region = "3" if region=="1, 3, 5"
replace region = "1" if pais==10004
destring region,replace
label define region 1 Europe 2 "Middle East" 3 Asia 4 Africa 5 Americas
label values region region
rename pais country
label define country 952 "QS" 96 "MK" 950 "QQ" 951 "QR" 660 "AF" 388 "ZA" 70 "AL" 10000 "DE" 43 "AD" 330 "AO" 446 "AI" 459 "AG" 478 "AN" 632 "SA" 208 "DZ" 528 "AR" 77 "AM" 474 "AW" 800 "AU" 38 "AT" 78 "AZ" 666 "BD" 469 "BB" 640 "BH" 17 "BE" 10003 "BELUX" 421 "BZ" 284 "BJ" 413 "BM" 10004 "USSR" 516 "BO" 93 "BA" 391 "BW" 508 "BR" 703 "BN" 68 "BG" 236 "BF" 328 "BI" 675 "BT" 247 "CV" 302 "CM" 696 "KH" 404 "CA" 644 "QA" 10004 "USSR" 21 "ILCA" 22 "CEUMEL" 244 "TD" 512 "CL" 720 "CN" 600 "CY" 45 "VA" 480 "CO" 375 "KM" 318 "CG" 724 "KP" 728 "KR" 272 "CI" 436 "CR" 92 "HR" 448 "CU" 8 "DK" 460 "DM" 220 "EG" 647 "AE" 500 "EC" 336 "ER" 91 "SI" 11 "ES" 400 "US" 53 "EE" 334 "ET" 823 "FM" 815 "FIJI" 708 "PH" 32 "FI" 1 "FR" 314 "GA" 252 "GM" 276 "GH" 625 "GAJE" 10004 "USSR" 44 "GI" 473 "GD" 9 "GR" 406 "GL" 458 "GP" 416 "GT" 488 "GY" 496 "GF" 260 "GN" 310 "GQ" 257 "GW" 452 "HT" 424 "HN" 740 "HK" 64 "HU" 653 "YE" 453 "BS" 463 "KY" 21 "ILCA" 529 "FK" 41 "FO" 820 "MP" 824 "MH" 806 "SB" 454 "TC" 468 "VG" 457 "VI" 811 "WF" 664 "IN" 700 "ID" 616 "IR" 612 "IQ" 7 "IE" 24 "IS" 624 "IL" 5 "IT" 464 "JM" 732 "JP" 338 "DJ" 628 "JO" 812 "KI" 636 "KW" 684 "LA" 395 "LS" 10004 "USSR" 604 "LB" 268 "LR" 216 "LY" 37 "LI" 10004 "USSR" 18 "LU" 743 "MO" 370 "MG" 701 "MY" 386 "MW" 667 "MV" 232 "ML" 46 "MT" 204 "MA" 462 "MQ" 373 "MU" 228 "MR" 377 "YT" 23 "MEL" 412 "MX" 366 "MZ" 74 "MD" 716 "MN" 470 "MS" 676 "MM" 389 "NA" 803 "NR" 672 "NP" 432 "NI" 240 "NE" 288 "NG" 28 "NO" 809 "NC" 804 "NZ" 810 "OCAM" 802 "OCAU" 814 "OCNZ" 649 "OM" 3 "NL" 958 "QU" 960 "QW" 959 "QV" 825 "PW" 442 "PA" 801 "PG" 662 "PK" 520 "PY" 504 "PE" 813 "PN" 822 "PF" 60 "PL" 10 "PT" 979 "QZ" 978 "QY" 977 "QX" 346 "KE" 83 "KG" 890 "AQ" 6 "GB" 306 "CF" 61 "CZ" 456 "DO" 63 "SK" 372 "RE" 66 "RO" 324 "RW" 75 "RU" 428 "SV" 819 "WS" 329 "SH" 465 "LC" 449 "KN" 47 "SM" 408 "PM" 311 "ST" 467 "VC" 248 "SN" 264 "SL" 94 "SERMON" 355 "SC" 706 "SG" 608 "SY" 342 "SO" 669 "LK" 393 "SZ" 224 "SD" 30 "SE" 39 "CH" 492 "SR" 27 "SJ" 680 "TH" 736 "TW" 82 "TJ" 352 "TZ" 357 "INDICO" 699 "TL" 280 "TG" 817 "TO" 472 "TT" 212 "TN" 80 "TM" 52 "TR" 807 "TV" 72 "UA" 350 "UG" 524 "UY" 81 "UZ" 816 "VU" 484 "VE" 690 "VN" 322 "CD" 378 "ZM" 382 "ZW" 
label values country country
rename year ano
collapse (max) intensity_level cumulative_intensity,by(country ano region)
drop if country==.
sort country ano
save acdata,replace

// Firm-year-country trade flows: attach information on war
use "`path'/computation/product_destination.dta",clear
keep if v_est2>0 & v_est2!=.
keep npc_alt ano v_est2 pais
collapse (sum) v_est2,by(npc_alt ano pais)
egen double share = pc(v_est2),by(npc_alt ano) prop
rename pais country
sort country ano
merge m:1 country ano using acdata
keep if _merge==3 | _merge==1
gen war = _merge==3 & cumulative_intensity>=1
gen war10 = _merge==3 & cumulative_intensity>=1 & share>=0.1 & share!=. 
gen war20 = _merge==3 & cumulative_intensity>=1 & share>=0.2 & share!=. 
gen war30 = _merge==3 & cumulative_intensity>=1 & share>=0.3 & share!=. 
drop _merge 
collapse (max) war war10 war20 war30,by(npc_alt ano)
sort npc_alt ano
save war_exp_data,replace

// Construct workers' export experience in war-torn countries
use C8,clear
keep nss ano npc_alt empresa
sort npc_alt ano
merge m:1 npc_alt ano using war_exp_data
keep if _merge==1 | _merge==3
replace war = 0 if _merge==1
replace war10 = 0 if _merge==1
replace war20 = 0 if _merge==1
replace war30 = 0 if _merge==1
drop _merge npc_alt
reshape wide empresa war*,i(nss) j(ano) 
local v=1996 
while `v'<=2006 {
local d = 1
while `d'<=`v'-1995 {
local s = `v' - `d'
if `d'==1 {
gen byte iwarexp`v' = empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & war`s'==1   /* dummy 0/1 : worked in other firms, that were war, in the past */
gen byte yriwarexp`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & war`s'==1   /* dummy 0/1 : worked in other firms, that were war, in the past */
gen byte iwarexp10`v' = empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & war10`s'==1   /* dummy 0/1 : worked in other firms, that were war, in the past */
gen byte yriwarexp10`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & war10`s'==1   /* dummy 0/1 : worked in other firms, that were war, in the past */
gen byte iwarexp20`v' = empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & war20`s'==1   /* dummy 0/1 : worked in other firms, that were war, in the past */
gen byte yriwarexp20`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & war20`s'==1   /* dummy 0/1 : worked in other firms, that were war, in the past */
gen byte iwarexp30`v' = empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & war30`s'==1   /* dummy 0/1 : worked in other firms, that were war, in the past */
gen byte yriwarexp30`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & war30`s'==1   /* dummy 0/1 : worked in other firms, that were war, in the past */
}
else {
replace iwarexp`v' = 1 if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & war`s'==1
replace yriwarexp`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & war`s'==1
replace iwarexp10`v' = 1 if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & war10`s'==1
replace yriwarexp10`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & war10`s'==1
replace iwarexp20`v' = 1 if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & war20`s'==1
replace yriwarexp20`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & war20`s'==1
replace iwarexp30`v' = 1 if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & war30`s'==1
replace yriwarexp30`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & war30`s'==1
}
local d = `d' +1
}
local v = `v' +1
}
drop war*
reshape long empresa iwarexp iwarexp10 iwarexp20 iwarexp30 yriwarexp yriwarexp10 yriwarexp20 yriwarexp30,i(nss) j(ano)
drop if empresa==.
// Fix issue of workers returning to old firms  
gen double minexpano = ano if iwarexp==1
egen double minexp=min(minexpano),by(nss)
gen warexp = (ano>=minexp)
gen byte yrwarexp = yriwarexp if ano>=minexp
sort nss ano
replace yrwarexp = yrwarexp[_n-1] + 1 if yrwarexp==. & ano>=minexp
recode warexp* (0=.) if ano==1995
recode yrwarexp* (0=.) if ano==1995
drop iwarexp minexp minexpano
gen double minexpano10 = ano if iwarexp10==1
egen double minexp10=min(minexpano10),by(nss)
gen warexp10 = (ano>=minexp10)
gen byte yrwarexp10 = yriwarexp10 if ano>=minexp10
sort nss ano
replace yrwarexp10 = yrwarexp10[_n-1] + 1 if yrwarexp10==. & ano>=minexp10
recode warexp10* (0=.) if ano==1995
recode yrwarexp10* (0=.) if ano==1995
drop iwarexp10 minexp10 minexpano10
gen double minexpano20 = ano if iwarexp20==1
egen double minexp20=min(minexpano20),by(nss)
gen warexp20 = (ano>=minexp20)
gen byte yrwarexp20 = yriwarexp20 if ano>=minexp20
sort nss ano
replace yrwarexp20 = yrwarexp20[_n-1] + 1 if yrwarexp20==. & ano>=minexp20
recode warexp20* (0=.) if ano==1995
recode yrwarexp20* (0=.) if ano==1995
drop iwarexp20 minexp20 minexpano20
gen double minexpano30 = ano if iwarexp30==1
egen double minexp30=min(minexpano30),by(nss)
gen warexp30 = (ano>=minexp30)
gen byte yrwarexp30 = yriwarexp30 if ano>=minexp30
sort nss ano
replace yrwarexp30 = yrwarexp30[_n-1] + 1 if yrwarexp30==. & ano>=minexp30
recode warexp30* (0=.) if ano==1995
recode yrwarexp30* (0=.) if ano==1995
drop iwarexp30 minexp30 minexpano30

compress
sort nss ano
save warexperience,replace
// firm-year level
gen r2warexp = warexp==1 & yrwarexp<=2
gen r3warexp = warexp==1 & yrwarexp<=3
gen r2warexp10 = warexp10==1 & yrwarexp10<=2
gen r3warexp10 = warexp10==1 & yrwarexp10<=3
gen r2warexp20 = warexp20==1 & yrwarexp20<=2
gen r3warexp20 = warexp20==1 & yrwarexp20<=3
gen r2warexp30 = warexp30==1 & yrwarexp30<=2
gen r3warexp30 = warexp30==1 & yrwarexp30<=3
collapse (max) warexp* r2warexp* r3warexp*,by(empresa ano)
sort empresa ano
save warexp_data,replace


log close
